<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

	public $postobj;
    public function index(){

    	if(isset($_GET['echostr'])){

    		if($this->checkSignature()){
	    		// file_put_contents('./db.txt', '888');
	    		echo $_GET['echostr'];
	    	}

    	}else{

    		$this->respson();
    	}

    	
    }

    public function respson()
    {
    	

    	$data = $GLOBALS['HTTP_RAW_POST_DATA'];

    	$this->postobj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);


    	switch ($this->postobj->MsgType) {
    		case 'text':
    			$this->respsonText();
    			break;
    		case 'image':
    			$this->respsonImg();
    			break;
    		case 'voice':
    			$this->respsonVoice();
    			break;
    		case 'video':
    			$this->respsonVodeo();
    			break;
    		case 'event':
    			$this->respsonEvento();
    			break;
    		default:
    			$this->respsonText();
    			break;
    	}

    	file_put_contents('./db.txt', json_encode());


    }
    public function respsonText()
    {

    	if($this->postobj->Content=='新闻'){
    		$this->respsonNews();exit;
    	}

    	$text = $this->robots($this->postobj->Content);
    	
    	$str = "<xml>
<ToUserName><![CDATA[".$this->postobj->FromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$this->postobj->ToUserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[".$text."]]></Content>
</xml>";
		
		echo $str;

    }

    public function respsonNews()
    {
    	$str = "<xml>
<ToUserName><![CDATA[".$this->postobj->FromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$this->postobj->ToUserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA["."今天好日子"."]]></Title> 
<Description><![CDATA["."真的是好日子"."]]></Description>
<PicUrl><![CDATA["."https://www.baidu.com/img/bd_logo1.png"."]]></PicUrl>
<Url><![CDATA["."http://www.baidu.com"."]]></Url>
</item>
<item>
<Title><![CDATA["."今天没有好吃的"."]]></Title>
<Description><![CDATA["."好像没有"."]]></Description>
<PicUrl><![CDATA[".'http://cms-bucket.nosdn.127.net/d406b04f3f384062b51d3acc0aafafae20171103152125.png?imageView&thumbnail=200y125&quality=85'."]]></PicUrl>
<Url><![CDATA[".'https://gitee.com/daniuit/p201703/blob/master/wx/Api/Home/Controller/IndexController.class.php'."]]></Url>
</item>
</Articles>
</xml>";
	echo $str;
    }

    public function respsonImg()
    {
    	$str = "<xml>
<ToUserName><![CDATA[".$this->postobj->FromUserName."]]></ToUserName>
<FromUserName><![CDATA[".$this->postobj->ToUserName."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[".$this->postobj->MediaId."]]></MediaId>
</Image>
</xml>";
		echo $str;
    }

    public function FunctionName($value='')
    {
    	# code...
    }

    public function checkSignature()
    {
    	// you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        		
		$token = TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
    }

    public function robots($w)
    {
    	header("Content-type: text/html; charset=utf-8");

    	$url = "http://www.tuling123.com/openapi/api";

    	$post_data = json_encode(array('key'=>'facc47b9f3afc7a493121bf6aacab53f','info'=>$w),JSON_UNESCAPED_UNICODE);

    	// var_dump($post_data);exit;

    	// var_dump($w);

    	$ch = curl_init();

    	curl_setopt($ch, CURLOPT_URL, $url);

    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    	curl_setopt($ch, CURLOPT_POST, 1);

    	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	    'Content-Type: application/json',
	    'Content-Length: ' . strlen($post_data)));

	    $out = curl_exec($ch);


	    $data =  json_decode($out,true);

	    if($data['code']==100000){
	    	return $data['text'];
	    }else{
	    	return "广西仔听不懂哦！！！！！";
	    }

    }
}

